package edu.xidian.onlinedocument.repository;


import edu.xidian.onlinedocument.entity.DocumentEditor;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface DocumentEditorJPARepository extends JpaRepository<DocumentEditor, Integer>, JpaSpecificationExecutor<DocumentEditor> {

    /**
     * exist
     */
    boolean existsDocumentEditorByDocumentData_DocId(int docId);

    boolean existsDocumentEditorByEditorData_UserId(int userId);

    boolean existsDocumentEditorByEditorData_UserNum(String userNum);

    boolean existsDocumentEditorByDocumentData_DocIdAndEditorData_UserId(int docId, int userId);

    boolean existsDocumentEditorByDocumentData_DocIdAndEditorData_UserIdAndDocEditorIsDeleteIsFalse(int docId, int userId);

    boolean existsDocumentEditorByDocumentData_DocIdAndEditorData_UserIdAndDocEditorIsDeleteIsTrue(int docId, int userId);

    /**
     * find
     */
    List<DocumentEditor> findAllByDocumentData_DocId(int docId);

    List<DocumentEditor> findAllByEditorData_UserId(int userId);

    List<DocumentEditor> findAllByEditorData_UserNum(String userNum);

    /**
     * 根据文档id查询未删除的文档
     * @param docId 文档id
     * @return 删除记号未标记的文档数据
     */
    List<DocumentEditor> findAllByDocumentData_DocIdAndDocEditorIsDeleteIsFalse(int docId);

    /**
     * 根据用户id查询未删除的文档
     * @param userId 用户id
     * @return 删除记号未标记的文档数据
     */
    List<DocumentEditor> findAllByEditorData_UserIdAndDocEditorIsDeleteIsFalse(int userId);

    /**
     * 根据文档id查询已删除的文档
     * @param docId 文档id
     * @return 删除记号已标记的文档数据
     */
    List<DocumentEditor> findAllByDocumentData_DocIdAndDocEditorIsDeleteIsTrue(int docId);

    /**
     * 根据用户id查询已删除的文档
     * @param userId 用户id
     * @return 删除记号已标记的文档数据
     */
    List<DocumentEditor> findAllByEditorData_UserIdAndDocEditorIsDeleteIsTrue(int userId);

    /**
     * 根据用户id和文档id对应的文档
     * @param docId 文档id
     * @param userId 用户id
     * @return 如果存在，则返回对象，不存在，返回null
     */
    DocumentEditor findDocumentEditorByDocumentData_DocIdAndEditorData_UserId(int docId, int userId);

    /**
     * 删除对应文档的所有操作
     * @param docId 文档id
     * @return 删除行数n（n>=0），如果返回值<0表示失败
     */
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    @Modifying
    int deleteAllByDocId(int docId);
}
